package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.adapter.ContributionReviewAdapter;
import com.lanou.lenanapp.beans.ContributionReview;
import com.lanou.lenanapp.volley.VolleySingleton;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 15/10/24.
 * 投稿评论
 */
public class ContributionReviewActivity extends Activity implements View.OnClickListener {

    private RecyclerView conreviewRecyclerView;
    private ContributionReviewAdapter contributionReviewAdapter;
    private GridLayoutManager manager;
    private String id;
    private ImageView contribution_back;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_review_contribution);

        getData();
        init();
        initData();

    }

    private void initData() {

        String url = "http://120.55.151.67/weibofun/users/ugc_comments_list.php?apiver=10700&pid=" + id +
                "&uid=-1&category=weibo_jokes&page=0&page_size=15&max_cid=-1&get_post=0&platform=iphone&appver=1.9&buildver=1.9.4&udid=0FD678F7-07AA-4EBF-BB39-EB79EAB40753&sysver=7.0.4";
        JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null && response.has("items")){

                    try {
                        JSONArray conrew = response.getJSONArray("items");
                        List<ContributionReview> contributionReviews = new ArrayList<ContributionReview>();
                        Gson gson = new Gson();
                        for (int i = 0; i < conrew.length(); i++){
                            String str = conrew.getString(i);
                            ContributionReview review = gson.fromJson(str, ContributionReview.class);
                            contributionReviews.add(review);
                        }
                        contributionReviewAdapter.addData(contributionReviews);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        volleySingleton.addRequest(request);

    }

    private void getData() {
        Intent intent = getIntent();
        id = intent.getStringExtra("conid");
    }

    private void init() {

        conreviewRecyclerView = (RecyclerView) findViewById(R.id.conRecycleView);
        contributionReviewAdapter = new ContributionReviewAdapter();
        manager = new GridLayoutManager(this, 1);
        conreviewRecyclerView.setLayoutManager(manager);
        conreviewRecyclerView.setAdapter(contributionReviewAdapter);
        contribution_back = (ImageView) findViewById(R.id.contribution_back);
        contribution_back.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        this.finish();
    }
}
